{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "alien_0={'color':'green','points':5}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "外星人的颜色是：green,\u0002t分数是5\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "print('外星人的颜色是：{},分数是{}'.format(alien_0['color'],alien_0['points']))\n",
    "print(alien_0['points'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['green', 5])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alien_0.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "alien_0['long']=5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['green', 5, 5])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alien_0.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "alien_0={'color':'green','points':5}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "del alien_0['color']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'points': 5}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alien_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_0={\n",
    "    'username':'efermi',\n",
    "    'first':'enrico',\n",
    "    'last':'fermi'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "key:username\n",
      "Value:efermi\n",
      "\n",
      "key:first\n",
      "Value:enrico\n",
      "\n",
      "key:last\n",
      "Value:fermi\n"
     ]
    }
   ],
   "source": [
    "for key,value in user_0.items():\n",
    "    print('\\nkey:'+key)\n",
    "    print('Value:'+value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['username', 'first', 'last'])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_0.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['efermi', 'enrico', 'fermi'])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_0.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'last'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Last'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "key.title()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "favorite_language={\n",
    "    'jen':'python',\n",
    "    'sarah':'c',\n",
    "    'edward':'ruby',\n",
    "    'phil':'python'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "frineds=['phil','sarah']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jen\n",
      "Sarah\n",
      "Hi Sarah , I see your favorite language is C！\n",
      "Edward\n",
      "Phil\n",
      "Hi Phil , I see your favorite language is Python！\n"
     ]
    }
   ],
   "source": [
    "for name in favorite_language.keys():\n",
    "    print(name.title())\n",
    "    if name in frineds:\n",
    "        print('Hi {} , I see your favorite language is {}！'.format(name.title(),favorite_language[name].title()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie_data = {\"宝贝当家\": [45, 2, 9, \"喜剧片\"],\n",
    "              \"美人鱼\": [21, 17, 5, \"喜剧片\"],\n",
    "              \"澳门风云3\": [54, 9, 11, \"喜剧片\"],\n",
    "              \"功夫熊猫3\": [39, 0, 31, \"喜剧片\"],\n",
    "              \"谍影重重\": [5, 2, 57, \"动作片\"],\n",
    "              \"叶问3\": [3, 2, 65, \"动作片\"],\n",
    "              \"伦敦陷落\": [2, 3, 55, \"动作片\"],\n",
    "              \"我的特工爷爷\": [6, 4, 21, \"动作片\"],\n",
    "              \"奔爱\": [7, 46, 4, \"爱情片\"],\n",
    "              \"夜孔雀\": [9, 39, 8, \"爱情片\"],\n",
    "              \"代理情人\": [9, 38, 2, \"爱情片\"],\n",
    "              \"新步步惊心\": [8, 34, 17, \"爱情片\"]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "def KNN(test_value,x):\n",
    "    distance=[]\n",
    "    for key,value in test_value.items():\n",
    "        d=math.sqrt((x[0]-value[0])**2+(x[1]-value[1])**2+(x[2]-value[2])**2)\n",
    "        distance.append([key,d])\n",
    "    return distance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "test1=KNN(movie_data,[23,3,17])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "test1=sorted(test1,key=lambda k:k[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['我的特工爷爷', 17.4928556845359],\n",
       " ['美人鱼', 18.547236990991408],\n",
       " ['功夫熊猫3', 21.470910553583888],\n",
       " ['宝贝当家', 23.430749027719962],\n",
       " ['澳门风云3', 32.14031735997639]]"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1=test1[:5]\n",
    "test1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels={\n",
    "    '喜剧片':0,\n",
    "    '动作片':0,\n",
    "    '爱情片':0\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "for s in test1:\n",
    "    label=movie_data[s[0]]\n",
    "    labels[label[3]]+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'喜剧片': 4, '动作片': 1, '爱情片': 0}\n"
     ]
    }
   ],
   "source": [
    "print(labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('喜剧片', 4), ('动作片', 1), ('爱情片', 0)]"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(labels.items(),key=lambda e:e[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['动作片', '喜剧片', '爱情片']"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(labels.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
